package com.zhongkai.jedis;


import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class JedisClientPool implements JedisClient {

    private JedisPool jedisPool;
    private Jedis jedis;
    private String result;

    public void setJedisPool(JedisPool jedisPool) {
        this.jedisPool = jedisPool;
    }

    @Override
    public synchronized String set(String key, String value) throws Exception {

        try {
            jedis = jedisPool.getResource();
            result = jedis.set(key, value);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return "500";
        } finally {
            if (jedis != null) {
                jedis.close();
            }

        }

    }

    @Override
    public synchronized String get(String key) throws Exception {

        try {
            jedis = jedisPool.getResource();
            result = jedis.get(key);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return "redis:500";
        } finally {
            if (jedis != null) {
                jedis.close();
            }

        }


    }

    @Override
    public synchronized Boolean exists(String key) throws Exception {

        try {
            jedis = jedisPool.getResource();
            Boolean result = jedis.exists(key);
            return result;

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (jedis != null) {
                jedis.close();
            }

        }


    }

    @Override
    public synchronized Long expire(String key, int seconds) throws Exception {

        try {
            jedis = jedisPool.getResource();
            Long result = jedis.expire(key, seconds);
            return result;

        } catch (Exception e) {
            e.printStackTrace();
            return 500l;
        } finally {
            if (jedis != null) {
                jedis.close();
            }

        }


    }

    @Override
    public Long ttl(String key) {
        jedis = jedisPool.getResource();
        Long result = jedis.ttl(key);
        jedis.close();
        return result;
    }

    @Override
    public Long incr(String key) {
        jedis = jedisPool.getResource();
        Long result = jedis.incr(key);
        jedis.close();
        return result;
    }

    @Override
    public Long hset(String key, String field, String value) {
        jedis = jedisPool.getResource();
        Long result = jedis.hset(key, field, value);
        jedis.close();
        return result;
    }

    @Override
    public String hget(String key, String field) {
        jedis = jedisPool.getResource();
        String result = jedis.hget(key, field);
        jedis.close();
        return result;
    }

    @Override
    public Long hdel(String key, String... field) {
        jedis = jedisPool.getResource();
        Long result = jedis.hdel(key, field);
        jedis.close();
        return result;
    }

    @Override
    public synchronized Long delete(String key) throws Exception {

        jedis = jedisPool.getResource();
        if (jedis.exists(key)) {
            return jedis.del(key);

        } else {
            return 100l;
        }


    }

    @Override
    public synchronized void setkeyexp(String key, String value, int exp) throws Exception {
        try {
            jedis = jedisPool.getResource();
            jedis.set(key, value);
            jedis.expire(key, exp);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedis != null) {
                jedis.close();
            }

        }
    }

}
